package com.wanxi.domain.code;


/**
 * 通用返回对象
 * Created by wx.mall on 2019/4/19.
 */
public class CommonResult<T> {
    private long code;
    private String message;
    private T data;
    private long total;

    protected CommonResult() {
    }

    protected CommonResult(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    protected CommonResult( String message, T data) {
        this.message = message;
        this.data = data;
    }

    public CommonResult(long code, String message,long total, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.total = total;
    }

    public CommonResult(long total, T data) {
        this.data = data;
        this.total = total;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> CommonResult<T> success(T data) {
        return data==null?new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), null):new CommonResult<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     *  @param data 获取的数据
     */


    public static <T> CommonResult<T> success(long total, T data) {
        CommonResult<T> objectCommonResult = new CommonResult<>(total,data);
        objectCommonResult.setCode(ResultCode.SUCCESS.getCode());
        objectCommonResult.setMessage(ResultCode.SUCCESS.getMessage());
        return objectCommonResult;
    }

    public static <T> CommonResult<T> success(String message, T data) {
        CommonResult<T> objectCommonResult = new CommonResult<>(message,data);
        objectCommonResult.setCode(ResultCode.SUCCESS.getCode());
        objectCommonResult.setMessage(message);
        return objectCommonResult;
    }

    public static <T> CommonResult<T> success() {
        CommonResult<T> objectCommonResult = new CommonResult<>();
        objectCommonResult.setCode(ResultCode.SUCCESS.getCode());
        objectCommonResult.setMessage(ResultCode.SUCCESS.getMessage());
        return objectCommonResult;
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     */
    public static <T> CommonResult<T> failed(IErrorCode errorCode) {
        return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> failed(String message) {
        return new CommonResult<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> CommonResult<T> failed() {
        return failed(ResultCode.FAILED);
    }

    /**
     * 添加失败返回结果
     */
    public static <T> CommonResult<T> addFailed() {
        return failed(ResultCode.ADD_FAILED);
    }


    /**
     * 参数验证失败返回结果
     */
    public static <T> CommonResult<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     * @param message 提示信息
     */
    public static <T> CommonResult<T> validateFailed(String message) {
        return new CommonResult<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    /**
     * 未登录返回结果
     */
    public static <T> CommonResult<T> unauthorized(T data) {
        return new CommonResult<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> CommonResult<T> forbidden(T data) {
        return new CommonResult<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }


    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}